package com.ibp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.ibp.entity.AppMgr;
import com.ibp.entity.ClusterConfMgr;
import com.ibp.entity.HostMgr;
import com.ibp.entity.NodeMgr;
import com.ibp.entity.ProjectMgr;
import com.ibp.service.AppMgrService;
import com.ibp.service.ClusterConfMgrService;
import com.ibp.service.FSHttpService;
import com.ibp.service.NodeMgrService;
import com.ibp.service.ProjectMgrService;

@Service("clusterConfMgrServiceImpl")														  
public class ClusterConfMgrServiceImpl extends BasicServiceImpl<ClusterConfMgr> implements ClusterConfMgrService{

	@Autowired 
	AppMgrService appMgrServiceImpl;
	
	@Autowired
	FSHttpService fSHttpServiceImpl ;
	
	@Autowired
	NodeMgrService nodeMgrServiceImpl;
	
	@Autowired
	ProjectMgrService projectMgrServiceImpl;
	/***
	 * 获取系统运行管理数据
	 * @param response
	 * @return
	 */
	public ClusterConfMgr getClusterConfMgr() {
		ClusterConfMgr ccMgr = new ClusterConfMgr();
		List data = new ArrayList<ClusterConfMgr>();
		int hostNum=0;
		int nodeNum=0;
		int projectNum=0;
		int appNum = 0;
		//获取节点数量
		NodeMgr filter=new NodeMgr();
		List<NodeMgr> listNodeMgr = new ArrayList<NodeMgr>();
		listNodeMgr=this.nodeMgrServiceImpl.getNodeMgrList(filter);
		nodeNum = listNodeMgr.size();
		//获取host数量
		for(NodeMgr temp:listNodeMgr){
			List<HostMgr> listHostMgr = new ArrayList<HostMgr>();
			if(temp.getHosts() == null){
				continue;
			}
			listHostMgr=HostMgrServiceImpl.getHostList(temp.getHosts(), "");
			hostNum = hostNum+ listHostMgr.size();
		}
		
		//获取app数量
		 List<AppMgr> appList = new ArrayList<AppMgr>();
		 AppMgr appMgr = null;
		 appList = this.appMgrServiceImpl.getAppMgrList(appMgr);
		 appNum=appList.size();
		 
		 //获取pro数量
		 List<ProjectMgr> proList = new ArrayList<ProjectMgr>();
		 ProjectMgr pm = new ProjectMgr();
		 proList = projectMgrServiceImpl.getProjectList(pm);
		 projectNum = proList.size();
		 
		 ccMgr.setAppAmount(appNum);
		 ccMgr.setHostAmount(hostNum);
		 ccMgr.setNodeAmount(nodeNum);
		 ccMgr.setRelationAmount(projectNum);
		return ccMgr;
	} 
	
	
	/***
	 * 解析响应文本
	 * @param response
	 * @return
	 */
	public List parseResponse(String response){
		if(response == null || "".equals(response)){
			return null ;
		}
		String[] res = response.split("\n");
		List list = new ArrayList<ClusterConfMgr>();
		for(int i=0;i<res.length;i++){
			String[] s = res[i].split(" "); 
			ClusterConfMgr srm = new ClusterConfMgr();
			for(int j=0;j<s.length;j++){
//				switch(j){
//				case 0 :
//					srm.setLocalUrl(s[j]);
//					break ;
//				case 1 :
//					srm.setSessionType(s[j]);
//					break ;	
//				case 2 :
//					srm.setSessionNode(s[j]);
//					break ;
//				case 3 :
//					srm.setRemoteUrl(s[j]);
//					break ;
//				}
			}
			list.add(srm);
		}
		return list ;
	}
	
	/***
	 * 判断注册中心的启停状态
	 * @param list
	 * @return
	 */
	public boolean getIbmsStatus(List list){
		return true;
	}
	
	
	public String getIbmsUrl(String url){
		
		return "172.16.68.110:16001";
	}
	
	
	public List<ClusterConfMgr> getList(ClusterConfMgr t) {
		return null;
	}

	public ClusterConfMgr get(ClusterConfMgr t) {
		return null;
	}

	public void add(ClusterConfMgr t) {
		
	}

	public void update(ClusterConfMgr t) {
		
	}

	public void delete(ClusterConfMgr t) {
		
	}

	public PageInfo<ClusterConfMgr> getPage(ClusterConfMgr t) {
		return null;
	}


	@Override
	public Object getAppList() {
		// TODO Auto-generated method stub
		return null;
	}
	   
}  